package com.scriverecanzoni.server;

import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.scriverecanzoni.client.PersistentService;
import com.scriverecanzoni.client.entities.Word;

@SuppressWarnings("serial")
public class PersistentServiceImpl extends RemoteServiceServlet implements
PersistentService {
    
    //Method must be present in Service Interface and Async Interface
    public void persistWord(String text, String textwacc) {
        
        //The Objectify service for the entity must be registered before any 
        //operations can be executed
        ObjectifyService.register(Word.class);
        Objectify ofy = ObjectifyService.begin();
        
        Word word = new Word(text,textwacc);
        
        ofy.put(word);
    }

    public ArrayList<Word> searchWord(String searchWord) {

		ObjectifyService.register(Word.class);
		Objectify ofy = ObjectifyService.begin();

		Query<Word> q = ofy.query(Word.class).filter("text",
				searchWord);

		ArrayList<Word> words = new ArrayList<Word>();

		//Loop the query results and add to the array
		for (Word fetched : q) {

			words.add(fetched);
		}

		return words;
	}
    
}
